<template>
  <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item label="库位" prop="config_id">
        <el-cascader
          v-model="form.config_id"
          placeholder="请选择库位"
          :options="selectData"
          clearable
          filterable
          :props="props"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item prop="tray_id">
        <span slot="label">
          托盘码
        </span>
        <el-input v-model="form.tray_id" />
      </el-form-item>

      <el-form-item prop="number">
        <span slot="label">
          入库数量
        </span>
        <el-input v-model="form.number" @input="numberChange" />
      </el-form-item>

      <el-form-item prop="box_number">
        <span slot="label">
          入库箱数
        </span>
        <el-input v-model="form.box_number" oninput="value=this.value.replace(/\D+/g,'');if(value.length>11)value=value.slice(0,11)" />
      </el-form-item>

      <el-form-item>
        <span slot="label">
          剩余商品入库数量
        </span>
        <el-input :value="surplus" disabled />
      </el-form-item>

      <el-form-item label="货物状态" prop="goods_status">
        <el-select v-model="form.goods_status" placeholder="货物状态" clearable size="small" style="width: 100%">
          <el-option label="Good" value="Good" />
          <el-option label="NG" value="NG" />
          <el-option label="Hold" value="Hold" />
        </el-select>
      </el-form-item>

      <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
        <el-col :span="24">
          <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
          <el-button size="medium" @click="close()">取消</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { trayAdd, trayEdit } from '@/api/storehouse'
import { getLocation,loacalConfig,getSon } from '@/api/stock'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    },
    warehousing_id: {
      type: String
    },
    warehousing_son_id: {
      type: String
    },
    surplus: {
      type: Number
    },
    packing_num: {
      type: Number
    }
  },
  data() {
    let self = this
    return {
      rules: {
        config_id: [
          { required: true, message: '请选择库位', trigger: 'change' }
        ],
        number: [
          { required: true, message: '请填写入库数量', trigger: 'blur' }
        ],
        box_number: [
          { required: true, message: '请填写入库箱数', trigger: 'blur' }
        ],
        goods_status: [
          { required: true, message: '请选择货物状态', trigger: 'change' }
        ]
      },
      selectData: [],
      packing_id: '',
      props: {
        expandTrigger: 'click',
        lazy: true,
        lazyLoad (node, resolve) {
          if(node.level==1){
            self.loacalConfig(2,node.value,resolve)
          }else if(node.level==2){
            self.loacalConfig(3,node.value,resolve)
          }else{
            resolve([])
          }
        }
      }
    }
  },
  created() {
    //this.getLocation()
    //this.loacalConfig(1)
  },
  methods: {
    loacalConfig(level,config_id,resolve) {
      const that = this
        if (level==3){
          getSon({config_id:config_id,checked:true}).then(response => {
            if (response.code === 10000) {
              var data = JSON.parse(JSON.stringify(response.data))
              for(let i in data) {
                var temp = { label: data[i].name, value: data[i].config_id }
                data[i] = temp
              }
              resolve(data)
            }
          })
        }else{
          loacalConfig({level:level,config_id:config_id}).then(response => {
            if (response.code === 10000) {
              if(level==1){
                that.selectData = JSON.parse(JSON.stringify(response.data))
                that.selectList(that.selectData)
              }else if(level==2){
                var data =  JSON.parse(JSON.stringify(response.data))
                data.forEach((item, index, arr)=>{
                  const temp = { label: item.name, value: item.config_id }
                  arr[index] = temp
                })
                resolve(data)
              }
            }
          })
        }
    },
    /** 查询库位 */
    getLocation() {
      const that = this
      getLocation().then(response => {
        if (response.code === 10000) {
          that.selectData = JSON.parse(JSON.stringify(response.data))
          that.selectList(that.selectData)
        }
      })
    },
    /* 处理数据 */
    selectList(data) {
      const replaceKey = function(data) {
        data.forEach(function(row, index, arr) {
          const temp = { label: row.name, value: row.config_id }
          if (row.children !== undefined) {
            if (row.children.length > 0) {
              temp['children'] = row.children
            }
            arr[index] = temp
            if (arr[index].children.length > 0) {
              replaceKey(arr[index].children)
            }
          } else {
            arr[index] = temp
          }
        })
      }
      replaceKey(data)
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      this.form.config_ids = this.form.config_id[this.form.config_id.length - 1]
      this.form.warehousing_id = this.warehousing_id
      this.form.warehousing_son_id = this.warehousing_son_id

      trayAdd(this.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      if (Array.isArray(this.form.config_id) === false) {
        this.form.config_ids = this.form.config_id
      } else if (Array.isArray(this.form.config_id) === true) {
        this.form.config_ids = this.form.config_id[this.form.config_id.length - 1]
      }
      this.form.warehousing_id = this.warehousing_id
      this.form.warehousing_son_id = this.warehousing_son_id
      trayEdit(this.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    },
    numberChange() {
      this.form.number = this.form.number.replace(/\D+/g, ''); if (this.form.number.length > 11) this.form.number = this.form.number.slice(0, 11)
      this.form.box_number = Math.ceil(this.form.number / this.packing_num)
    }
  }
}
</script>

